package yzbx.personalwebsite.service;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import yzbx.personalwebsite.entity.HotTodayEntity;
import yzbx.personalwebsite.tool.Httptool;
import yzbx.personalwebsite.tool.RedisTool;

import java.util.List;

import static yzbx.personalwebsite.tool.RedisTool.REDIS_SAVE_TIME_5M;

/**
 * {@code @Author} zxm
 * {@code @Description} 获取热点数据服务类
 * {@code @Date} 2024/09/29/11:23
 * {@code @Version} 1.0
 */
public abstract class GetHotTodayService {

    private static final Logger LOGGER = LoggerFactory.getLogger(GetHotTodayService.class);

    // 抓取数据最大数量
    protected final int DATA_NUM_MAX = 10;

    // redis工具类
    protected final RedisTool redisTool;

    // http工具类
    protected final Httptool httptool;

    @Autowired
    // Spring4.0后推荐构造器注入Bean
    public GetHotTodayService(RedisTool redisTool, Httptool httptool) {
        this.redisTool = redisTool;
        this.httptool = httptool;
    }

    public List<HotTodayEntity> GetHotToday(String logTheme, String key) {
        List<HotTodayEntity> hotTodayList = redisTool.getValue(logTheme, key);
        if (!hotTodayList.isEmpty()) {
            return hotTodayList;
        }
        hotTodayList = GetData(logTheme);
        redisTool.setValue(logTheme, key, hotTodayList, REDIS_SAVE_TIME_5M);
        return hotTodayList;
    }

    public abstract List<HotTodayEntity> GetData(String logTheme);
}
